/*  yALB - Yet Another List Builder (roster Editor for Wargames/tabletop games).
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

#include "gameStructs.h"
#include <QMessageBox>

const int KeyMaxSize = 15;

bool GameStructs::isValidKey(const QString& key) {
    if(key.isEmpty()) {
        QMessageBox::critical(0, tr("Error"), tr("The ID is empty"));
        return false;
    }

    if(key.size() > KeyMaxSize) {
        QMessageBox::critical(0, tr("Error"), tr("The ID can not contain more than %1 characters").arg(KeyMaxSize));
        return false;
    }

    QRegExp reg(":|,|\"|>|!|\\s");
    if(reg.indexIn(key) > -1) {
        QMessageBox::critical(0, tr("Error"), tr("The ID can not contain :,>,!,\" commas or spaces."));
        return false;
    }
    return true;
}

// \TODO: Move options like this to their respective classes

UnitOption::InitialState GameStructs::getOptionStateFromInt(int initialState) {
    if(initialState >= 0 && initialState < UnitOption::ElementCount)
        return UnitOption::InitialState(initialState);
    else
        return UnitOption::InitialState(0);
}

QString GameStructs::getValidName(const QString &name) {
    if(name.isEmpty())
        return tr("No name");
    else
        return name;
}

// GameCategory struct

GameCategory::GameCategory() {
    name = GameStructs::getValidName("");
    numericLimit = 0;
    pointLimit = 0;
    percentageLimit = 0;
}

GameCategory::GameCategory(const QString &name, uint numericLimit,
                           uint pointLimit, uint percentageLimit) {
    this->name = GameStructs::getValidName(name);
    setNumericLimit(numericLimit);
    setPointLimit(pointLimit);
    setPercentageLimit(percentageLimit);
}

// Column struct

Column::Column() {
    name = GameStructs::getValidName("");
    visible = true;
}

Column::Column(const QString& name, const QString& defaultValue,
       const QString& defaultValueAs, bool visible) {
    this->name = GameStructs::getValidName(name);
    this->defaultValue = defaultValue;
    this->defaultValueAs = defaultValueAs;
    this->visible = visible;
}

// GameProperty struct

GameProperty::GameProperty() {
    name = GameStructs::getValidName("");
}

GameProperty::GameProperty(const QString &name, const QString &defaultValue) {
    this->name = GameStructs::getValidName(name);
    this->defaultValue = defaultValue;
}

// UnitOption struct

UnitOption::UnitOption() {
    name = GameStructs::getValidName("");
    setCost(0);
    costPerMin = false;
}

UnitOption::UnitOption(const QString &categoryKey, UnitOption::InitialState initialState,
                       const QString &name, float cost, bool costPerMin,
                       const QString &required,
                       const Actions &actions,
                       const QHash<QString, QString> &properties) {

    this->category = categoryKey;
    this->state = UnitOption::InitialState(initialState);
    this->name = GameStructs::getValidName(name);
    setCost(cost);
    this->costPerMin = costPerMin;
    this->required = required;
    this->actions = actions;
    this->properties = properties;
}

// GameItem struct

GameItem::GameItem() {
    name = GameStructs::getValidName("");
    setCost(0);
    maximumNumber = 0;
    costPerMin = false;
}

GameItem::GameItem(const QString &categoryKey, const QString &name, float cost, bool costPerMin,
         uint maximumNumber, const Actions &actions,
         const QHash<QString, QString>& properties) {
    this->category = categoryKey;
    this->name = GameStructs::getValidName(name);
    this->costPerMin = costPerMin;
    setCost(cost);
    setMaximumNumber(maximumNumber);
    this->actions = actions;
    this->properties = properties;
}

// GlobalText struct

GlobalText::GlobalText() {
    name = GameStructs::getValidName("");
}

GlobalText::GlobalText(const QString& name, const QString& text) {
    this->name = GameStructs::getValidName(name);
    this->text = text;
}
